﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Meqs.Entity;
using Meqs.Logic.Interface;
using System.Collections.Generic;

public partial class Base_School_upload : System.Web.UI.Page
{
    private int GetGrade
    {
        get {
            string grade = Request.QueryString["grade"];
            int temp = 0;
            if (int.TryParse(grade, out temp))
            {
                return temp;
            }
            return 0;
        }
    }
    protected void btnReqClsOK_ServerClick(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            try
            {
                //  保存文件
                DataSet ds = Meqs.Utilities.ExcelUtil.ExcelImport(FileUpload1.PostedFile.InputStream);
                if (ds.Tables.Count > 0)
                {
                    Hashtable ht = new Hashtable();
                    //ht.Add("Grade", GetGrade);
                    ht.Add("SchoolMark", Common.GetConstSchoolMark());
                    ht.Add("IsEffect", 1);
                    //当前有班主任的班级
                    IList<VClsInfo> vc = Container<IClsInfoLogic>.Instance.FindClassInfo(ht);

                    //当前年级所有的有效班
                    IList<ClsInfo> clses = Container<IClassHistoryLogic>.Instance.FindClassesInSchoolYear(SchoolCommon.Sch_SchoolInfo.SchoolYear, Common.GetConstSchoolMark(), GetGrade);


                    //当前年级各班各科的教师配备情况。
                    IList<ClassTercher> classteachers = Container<ITeachClsCauseLogic>.Instance.FindClassTercher(GetGrade,1);
                    SchoolInfo si = SchoolCommon.Sch_SchoolInfo;

                    ht.Clear();
                    ht.Add("Iseffect", 1);
                    IList<GlobalCauseInfo> gcis = Container<IGlobalCauseInfoLogic>.Instance.FindAll(ht);
                    //挑选出不符合的人进行填充。
                    int rowindex = 0;
                    foreach (DataRow row in ds.Tables[0].Rows)
                    {
                        for (int i = 1; i < ds.Tables[0].Columns.Count; i++)
                        {
                            if (rowindex == 0)
                            {
                                //班主任
                                //设置班主任,年级组，以及权限。
                                if (!(row[i] is DBNull) )
                                {
                                    string tercher = row[i].ToString().Replace(" ","").ToLower();
                                    if (!string.IsNullOrEmpty(tercher))
                                    {
                                        //查出新教师的信息
                                        ht.Clear();
                                        ht.Add("FullTeachName", tercher);
                                        ht.Add("IsEffect", 1);
                                        IList<TeacherInfo> tis = Container<ITeacherInfoLogic>.Instance.FindAll(ht);
                                        foreach (VClsInfo clsi in vc)
                                        {
                                            if (clsi.Header == null)
                                                continue;
                                            if (clsi.ClsGradeName == ds.Tables[0].Columns[i].Caption && tercher != clsi.Header.Replace(" ", "").ToLower())
                                            {
                                                
                                                ht.Clear();
                                                ht.Add("FullTeachName", clsi.Header.Replace(" ", "").ToLower());
                                                ht.Add("IsEffect", 1);
                                                IList<TeacherInfo> tis2 = Container<ITeacherInfoLogic>.Instance.FindAll(ht);
                                                if (tis.Count > 0)
                                                {
                                                    //查出现任班主任本年度有还担任几个班的班主任，小于一个，去掉班主任权限。
                                                    ht.Clear();
                                                    ht.Add("HeaderID", tis2[0].RID);
                                                    ht.Add("IsEffect", 1);
                                                    IList<ClsInfo> classesinfos = Container<IClsInfoLogic>.Instance.FindAll(ht);
                                                    if (classesinfos.Count <= 1)
                                                    {
                                                        ht.Clear();
                                                        ht.Add("TeacherID", tis2[0].RID);
                                                        ht.Add("SchoolYear",si.SchoolYear);
                                                        ht.Add("Term", si.SchoolTerm);
                                                        ht.Add("RoleID", (int)RoleKeyEnum.Class_Header);
                                                        ht.Add("SchoolMark", Common.GetConstSchoolMark());
                                                        Container<ITeacherRolesLogic>.Instance.DeleteBy(ht);

                                                        
                                                    }
                                                }
                                                

                                            }
                                        }
                                        if (tis.Count > 0)
                                        {
                                            ht.Clear();
                                            ht.Add("TeacherID", tis[0].RID);
                                            ht.Add("SchoolYear", si.SchoolYear);
                                            ht.Add("Term", si.SchoolTerm);
                                            ht.Add("RoleID", (int)RoleKeyEnum.Class_Header);
                                            ht.Add("SchoolMark", Common.GetConstSchoolMark());
                                            IList<TeacherRoles> tr = Container<ITeacherRolesLogic>.Instance.FindAll(ht);
                                            if (tr.Count < 1)
                                            {
                                                TeacherRoles t = new TeacherRoles();
                                                t.RoleID = (int)RoleKeyEnum.Class_Header;
                                                t.SchoolMark = si.SchoolMark;
                                                t.SchoolYear = si.SchoolYear;
                                                t.TeacherID = tis[0].RID;
                                                t.Term = si.SchoolTerm;
                                                Container<ITeacherRolesLogic>.Instance.Create(t);
                                            }
                                            ht.Clear();
                                            ht.Add("TeacherID", tis[0].RID);
                                            ht.Add("SchoolYear", si.SchoolYear);
                                            ht.Add("Term", si.SchoolTerm);
                                            ht.Add("Grade", GetGrade);
                                            ht.Add("SchoolMark", Common.GetConstSchoolMark());
                                            IList<GradeGroup> group = Container<IGradeGroupLogic>.Instance.FindAll(ht);
                                            if (group.Count < 1)
                                            {
                                                GradeGroup g = new GradeGroup();
                                                g.Grade = GetGrade;
                                                g.SchoolMark = Common.GetConstSchoolMark();
                                                g.SchoolYear = si.SchoolYear;
                                                g.Term = si.SchoolTerm;
                                                g.TeacherID = tis[0].RID;
                                                Container<IGradeGroupLogic>.Instance.Create(g);
                                            }
                                            foreach (ClsInfo newcls in clses)
                                            {
                                                if (newcls.ClsName == ds.Tables[0].Columns[i].Caption)
                                                {
                                                    newcls.HeaderID = tis[0].RID;
                                                    newcls.SchoolMark = Common.GetConstSchoolMark();
                                                    Container<IClsInfoLogic>.Instance.UpdateClsExceptClsNO(newcls);
                                                    break;
                                                }
                                            }
                                        }
                                    }

                                }

                            }
                            else
                            { 
                                //任课教师
                                //设置任课教师，年级组，教研组，以及权限。
                                if (!(row[i] is DBNull))
                                {
                                    string tercher = row[i].ToString().Replace(" ","").ToLower();
                                    if (!string.IsNullOrEmpty(tercher))
                                    {
                                        //查出新教师的信息
                                        ht.Clear();
                                        ht.Add("FullTeachName", tercher);
                                        ht.Add("IsEffect", 1);
                                        IList<TeacherInfo> tis = Container<ITeacherInfoLogic>.Instance.FindAll(ht);
                                        foreach (ClassTercher ctc in classteachers)
                                        {
                                            if (ctc.ClsName == ds.Tables[0].Columns[i].Caption && ctc.CauseName == row[0].ToString().Replace(" ", "").ToLower() && tercher != ctc.TeachName.Replace(" ", "").ToLower())
                                            {
                                               
                                                if (tis.Count > 0)
                                                {
                                                    //将原来教师的班的任课记录去除掉。
                                                    ht.Clear();
                                                    ht.Add("TeachID", ctc.TeacherID);
                                                    ht.Add("ClsID", ctc.ClsID);
                                                    ht.Add("CauseID", ctc.CauseID);
                                                    Container<ITeachClsCauseLogic>.Instance.DeleteClsCauseTeacher(ht);
                                                    //TeachClsCause t = new TeachClsCause(tis[0].RID, ctc.CauseID, ctc.ClsID);
                                                    //Container<ITeachClsCauseLogic>.Instance.Create(t);
                                                }
                                            }
                                        }

                                        //加入教研组，任课信息，年级组，权限信息。
                                        int gciid = 0;
                                        int clsid = 0;
                                        foreach(GlobalCauseInfo gci in gcis)
                                        {
                                            if(gci.CauseName == row[0].ToString().Replace(" ", "").ToLower())
                                            {
                                                gciid = gci.RID;
                                                

                                            }
                                        }
                                        foreach (ClsInfo ci in clses)
                                        {
                                            if (ci.ClsName == ds.Tables[0].Columns[i].Caption.Replace(" ", "").ToLower())
                                            {
                                                clsid = ci.RID;
                                            }
                                        }
                                        //加入教研组
                                        ht.Clear();
                                        ht.Add("TeachID", tis[0].RID);
                                        ht.Add("ClsID", 0);
                                        ht.Add("CauseID", gciid);
                                        IList<TeachClsCause> temp = Container<ITeachClsCauseLogic>.Instance.FindAll(ht);
                                        if (temp.Count == 0)
                                        {
                                            TeachClsCause tcc = new TeachClsCause(tis[0].RID, gciid, 0);
                                            Container<ITeachClsCauseLogic>.Instance.InsertTeachClsCause(tcc);
                                        }
                                        //任课信息
                                        ht.Clear();
                                        ht.Add("TeachID", tis[0].RID);
                                        ht.Add("ClsID", clsid);
                                        ht.Add("CauseID", gciid);
                                        temp = Container<ITeachClsCauseLogic>.Instance.FindAll(ht);
                                        if (temp.Count == 0)
                                        {
                                            TeachClsCause tcc = new TeachClsCause(tis[0].RID, gciid, clsid);
                                            Container<ITeachClsCauseLogic>.Instance.InsertTeachClsCause(tcc);
                                        }
                                        //年级组
                                        ht.Clear();
                                        ht.Add("TeacherID", tis[0].RID);
                                        ht.Add("SchoolYear", si.SchoolYear);
                                        ht.Add("Term", si.SchoolTerm);
                                        ht.Add("Grade", GetGrade);
                                        ht.Add("SchoolMark", Common.GetConstSchoolMark());
                                        IList<GradeGroup> group = Container<IGradeGroupLogic>.Instance.FindAll(ht);
                                        if (group.Count < 1)
                                        {
                                            GradeGroup g = new GradeGroup();
                                            g.Grade = GetGrade;
                                            g.SchoolMark = Common.GetConstSchoolMark();
                                            g.SchoolYear = si.SchoolYear;
                                            g.Term = si.SchoolTerm;
                                            g.TeacherID = tis[0].RID;
                                            Container<IGradeGroupLogic>.Instance.Create(g);
                                        }
                                        //权限
                                        ht.Clear();
                                        ht.Add("TeacherID", tis[0].RID);
                                        ht.Add("SchoolYear", si.SchoolYear);
                                        ht.Add("Term", si.SchoolTerm);
                                        ht.Add("RoleID", (int)RoleKeyEnum.Class_Teacher);
                                        ht.Add("SchoolMark", Common.GetConstSchoolMark());
                                        IList<TeacherRoles> tr = Container<ITeacherRolesLogic>.Instance.FindAll(ht);
                                        if (tr.Count < 1)
                                        {
                                            TeacherRoles t = new TeacherRoles();
                                            t.RoleID = (int)RoleKeyEnum.Class_Teacher;
                                            t.SchoolMark = si.SchoolMark;
                                            t.SchoolYear = si.SchoolYear;
                                            t.TeacherID = tis[0].RID;
                                            t.Term = si.SchoolTerm;
                                            Container<ITeacherRolesLogic>.Instance.Create(t);
                                        }
                                    }

                                }
                            }
                        }
                        rowindex++;
                    }
                    Response.Write("<script>parent.HaveDone(1,'')</script>");
                }
                else
                {
                    //错误文件
                    Response.Write("<script>parent.HaveDone(2,'无法获取数据，请使用模版进行填报！')</script>");
                }
            }
            catch (Exception ex)
            {
                Response.Write("<script>parent.HaveDone(2,'" + ex.Message + "')</script>");
            }
        }
        else
        {
            Response.Write("<script>parent.HaveDone(2,'文件错误！')</script>");
        }

    }
}
